/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package protocol;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.n3.nanoxml.*;

/**
 *
 * @author ylopez
 */
public class SRDProtocol{
    private String message;

    public SRDProtocol(){
        this.message = null;
    }

    public SRDProtocol(String messageAction, String[] parameters){
        if (messageAction.equals("connection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"connection\">" +
                        "<user>" +
                            "<name>"+ parameters[0] +"</name>" +
                            "<time>"+ parameters[1] +"</time>" +
                        "</user>" +
                    "</Messages>";
        } else if (messageAction.equals("replyConnection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyConnection\">" +
                        "<status>"+ parameters[0] +"</status>" +
                    "</Messages>";
        } else if (messageAction.equals("replyReplyConnection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyReplyConnection\">" +
                        "<status> OK OK </status>" +
                    "</Messages>";
        } else if (messageAction.equals("rdir")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"rdir\" />";
        } else if (messageAction.equals("replyRdir")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyRdir\">";
            for (int i = 0; i < parameters.length; i = i +2){
                this.message = this.message +
                        "<file>" +
                            "<name>"+ parameters[i] +"</name>" +
                            "<size>"+ parameters[i+1] +"</size>" +
                        "</file>";
            }
            this.message = this.message +
                    "</Messages>";
        } else if (messageAction.equals("tuser")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"tuser\" />";
        }  else if (messageAction.equals("replyTuser")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyTuser\">";
            for (int i = 0; i < parameters.length; i = i +2){
                this.message = this.message +
                        "<user>" +
                            "<name>"+ parameters[i] +"</name>" +
                            "<time>"+ parameters[i+1] +"</time>" +
                        "</user>";
            }
            this.message = this.message +
                    "</Messages>";
        } else if (messageAction.equals("drepConnection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"drepConnection\">" +
                        "<ip>"+ parameters[0] +"</ip>" +
                        "<time>"+ parameters[1] +"</time>" +
                    "</Messages>";
        } else if (messageAction.equals("replyDrepConnection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyDrepConnection\">" +
                        "<status>"+ parameters[0] +"</status>" +
                    "</Messages>";
        } else if (messageAction.equals("replyReplyDrepConnection")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyReplyDrepConnection\">" +
                        "<status> OK OK </status>" +
                    "</Messages>";
        } else if (messageAction.equals("nrep")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"nrep\" />";
        } else if (messageAction.equals("replyNrep")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyNrep\">" +
                        "<nrep>"+ parameters[0] +"</nrep>" +
                    "</Messages>";
        } else if (messageAction.equals("drepDisconnect")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"drepDisconnect\" />";
        } else if (messageAction.equals("replyDrepDisconnect")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyDrepDisconnect\">" +
                        "<status> OK</status>" +
                    "</Messages>";
        } else if (messageAction.equals("disconnect")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"disconnect\">" +
                        "<user>" +
                            "<name>"+ parameters[0] +"</name>" +
                            "<time>"+ parameters[1] +"</time>" +
                        "</user>" +
                    "</Messages>";
        } else if (messageAction.equals("replyDisconnect")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyDisconnect\">" +
                        "<status> OK </status>" +
                    "</Messages>";
        } else if (messageAction.equals("sube")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"sube\">" +
                        "<file name=\""+ parameters[0] +"\" size=\""+ parameters[1] +"\">" +
                        "</file>" +
                    "</Messages>";
        } else if (messageAction.equals("replySube")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replySube\">" +
                        "<status> OK </status>" +
                    "</Messages>";
        } else if (messageAction.equals("existFile")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"existFile\">" +
                        "<name>"+ parameters[0] +"</name>" +
                    "</Messages>";
        } else if (messageAction.equals("replyExistFile")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"replyExistFile\">" +
                        "<status>"+ parameters[0] +"</status>" +
                        "<size>"+ parameters[1] +"</size>" +
                    "</Messages>";
        } else if (messageAction.equals("baja")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"baja\">" +
                        "<name>"+ parameters[0] +"</name>" +
                    "</Messages>";
        } else if (messageAction.equals("sendFile")) {
            this.message =
                    "<Messages protocol= \"PSRD\" version= \"1.0\" action= \"sendFile\">" +
                        "<file name=\""+ parameters[0] +"\" size=\""+ parameters[1] +"\">" +
                            parameters[2] +
                        "</file>" +
                    "</Messages>";
        }
    }
    //TODO completar estas funciones y los casos para sube y baja del protocolo
    public String getMessage(){
        return this.message;
    }
    
    public void setMessage(String message){
        this.message = message;
    }
    
    public String getMessageAction(){
        IXMLParser parser;
        String messageAction = null;
        try {
            parser = XMLParserFactory.createDefaultXMLParser();
            IXMLReader reader;
            reader = StdXMLReader.stringReader(this.getMessage());
            parser.setReader(reader);
            IXMLElement xml = (IXMLElement) parser.parse();
            messageAction = xml.getAttributes().getProperty("action");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XMLException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        }

        return messageAction;
    }
    
    public String[] processMessage(){
        String [] resoults = null;
        String messageAction = this.getMessageAction();

        IXMLParser parser;
        try {
            parser = XMLParserFactory.createDefaultXMLParser();
            IXMLReader reader;
            reader = StdXMLReader.stringReader(this.getMessage());
            parser.setReader(reader);
            IXMLElement xml = (IXMLElement) parser.parse();

            if (messageAction.equals("connection") || messageAction.equals("replyTuser") || messageAction.equals("disconnect")) {
                Vector users = xml.getChildrenNamed("user");
                IXMLElement user;
                int i = 0;
                resoults = new String[2*users.size()];
                while (i<users.size()) {
                    user = (IXMLElement) users.get(i);
                    resoults[2*i] = user.getChildAtIndex(0).getContent();
                    resoults[2*i+1] = user.getChildAtIndex(1).getContent();
                    i ++;
                }
            }

            if (messageAction.equals("replyRdir")) {
                Vector serverFiles = xml.getChildrenNamed("file");
                IXMLElement file;
                int i = 0;
                resoults = new String[2*serverFiles.size()];
                while (i<serverFiles.size()) {
                    file = (IXMLElement) serverFiles.get(i);
                    resoults[2*i] = file.getChildAtIndex(0).getContent();
                    resoults[2*i+1] = file.getChildAtIndex(1).getContent();
                    i ++;
                }
            }

            if (messageAction.equals("replyNrep")){
                IXMLElement nrep = xml.getFirstChildNamed("nrep");
                resoults = new String[]{nrep.getContent()};
            }

            if (messageAction.equals("replyConnection") || messageAction.equals("replyDrepConnection") 
                    || messageAction.equals("replySube")){
                IXMLElement status = xml.getFirstChildNamed("status");
                resoults = new String[]{status.getContent()};
            }

            if (messageAction.equals("sube")) {
                IXMLElement file = xml.getFirstChildNamed("file");
                resoults = new String[3];
                resoults[0] = file.getAttributes().getProperty("name");
                resoults[1] = file.getAttributes().getProperty("size");
                resoults[2] = file.getContent();
            }

            if (messageAction.equals("existFile") || messageAction.equals("baja")) {
                IXMLElement nameFile = xml.getFirstChildNamed("name");
                resoults = new String[]{nameFile.getContent()};
            }

            if (messageAction.equals("replyExistFile")) {
                IXMLElement status = xml.getFirstChildNamed("status");
                IXMLElement size = xml.getFirstChildNamed("size");
                resoults = new String[]{status.getContent(), size.getContent()};
            }

            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XMLException ex) {
            Logger.getLogger(SRDProtocol.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resoults;
    }
}